package com.xmim.fulcrum;

import com.xmim.core.container.ServerContainer;
import com.xmim.core.session.EventType;
import com.xmim.core.session.SessionListener;
import com.xmim.core.util.SessionUtil;
import com.xmim.fulcrum.session.FulcrumSession;

import java.util.Enumeration;
import java.util.List;
import java.util.Map;

public class FulcrumUtil extends SessionUtil {
    public static void addHandle(ServerContainer parent, FulcrumSession session) {
        Enumeration<Map.Entry> enumerations = parent.enumerations();
        while (enumerations.hasMoreElements()) {
            Map.Entry element = enumerations.nextElement();
            if (session.checkHandler(element.getValue().getClass())) {
                session.addHandler(element.getKey(), element.getValue());
            }
        }
    }

    public static void triggerAdd(List<SessionListener<FulcrumSession>> sessionListeners, FulcrumSession session) {
        for (SessionListener<FulcrumSession> sessionListener : sessionListeners) {
            sessionListener.event(EventType.ADDED, session);
        }
    }

    public static FulcrumSession triggerFind(List<SessionListener<FulcrumSession>> sessionListeners, FulcrumSession fulcrumSession) {
        FulcrumSession session = null;
        for (SessionListener<FulcrumSession> sessionListener : sessionListeners) {
            session = sessionListener.event(EventType.FIND, session);
        }
        return session;
    }

    public static void triggerDestroy(List<SessionListener<FulcrumSession>> sessionListeners, FulcrumSession session) {
        for (SessionListener<FulcrumSession> sessionListener : sessionListeners) {
            sessionListener.event(EventType.DESTROY, session);
        }
    }
}
